package com.gec.anan.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.anan.driver.mapper.DriverAccountDetailMapper;
import com.gec.anan.driver.mapper.DriverAccountMapper;
import com.gec.anan.driver.service.DriverAccountService;
import com.gec.anan.model.entity.driver.DriverAccount;
import com.gec.anan.model.entity.driver.DriverAccountDetail;
import com.gec.anan.model.form.driver.TransferForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverAccountServiceImpl extends ServiceImpl<DriverAccountMapper, DriverAccount> implements DriverAccountService {


    @Autowired
    private DriverAccountMapper driverAccountMapper;
    @Autowired
    private DriverAccountDetailMapper driverAccountDetailMapper;
    //将系统奖励转账到司机账户中
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean transfer(TransferForm transferForm) {
        //去重，判段detail表是否存在
        LambdaQueryWrapper<DriverAccountDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverAccountDetail::getTradeNo, transferForm.getTradeNo());
        Long count = driverAccountDetailMapper.selectCount(queryWrapper);
        if(count>0){
            return true;
        }
        //添加系统奖励
        driverAccountMapper.add(transferForm.getDriverId(),transferForm.getAmount());
        //添加detail表记录
        DriverAccountDetail driverAccountDetail = new DriverAccountDetail();
        BeanUtils.copyProperties(transferForm,driverAccountDetail);
        driverAccountDetailMapper.insert(driverAccountDetail);
        return true;
    }
}
